
package com.hexinfo.dmpro.sparing.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.utils.CommonConstants;
import com.hexinfo.dmpro.common.utils.ScanCommonConstants;
import com.hexinfo.dmpro.sparing.model.HotStandbyTable;
import com.hexinfo.dmpro.sparing.dao.HotStandbyTableMapper;
import com.hexinfo.dmpro.sparing.service.HotStandbyTableService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 热备表级同步记录表
 *
 * @author yemw
 * @date 2024-04-28 14:19:29
 */
@Service
public class HotStandbyTableServiceImpl extends ServiceImpl<HotStandbyTableMapper, HotStandbyTable> implements HotStandbyTableService {

    @Override
    public Page queryList(Page page, HotStandbyTable hotStandbyTable) {
        LambdaQueryWrapper<HotStandbyTable> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(HotStandbyTable::getDel,CommonConstants.STATUS_NORMAL)
                .like(HotStandbyTable::getTaskDate,hotStandbyTable.getTaskDate())
                .like(HotStandbyTable::getSourceName,hotStandbyTable.getSourceName())
                .like(HotStandbyTable::getDbTbName,hotStandbyTable.getDbTbName())
                .like(HotStandbyTable::getTaskState,hotStandbyTable.getTaskState())
                .apply(StrUtil.isNotBlank(hotStandbyTable.getStartTime()),"CREATE_TIME between to_date('" + hotStandbyTable.getStartTime() + "','yyyy-MM-dd HH24:MI:SS') and to_date('"+ hotStandbyTable.getEndTime() + "','yyyy-MM-dd HH24:MI:SS')")
                .orderByDesc(HotStandbyTable::getCreateTime);
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        return returnPage;
    }

    @Override
    public boolean remove(String id) {
        HotStandbyTable hotStandbyTable = new HotStandbyTable();
        hotStandbyTable.setId(id);
        hotStandbyTable.setDel(CommonConstants.STATUS_DEL);
        int rem = this.baseMapper.updateById(hotStandbyTable);
        if(rem>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<HotStandbyTable> querySourceName(String sourceName,String taskDate) {
        LambdaQueryWrapper<HotStandbyTable> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(HotStandbyTable::getSourceName,sourceName)
                .eq(HotStandbyTable::getTaskDate,taskDate)
                .like(HotStandbyTable::getTaskState, ScanCommonConstants.ClusterName.SB.value);
        List<HotStandbyTable> hotStandbyTables = this.baseMapper.selectList(wrapper);
        return hotStandbyTables;
    }
}
